package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Date;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Listeners.SubmitNewPaperListener;
import Model.Conference;
import Model.User;

/**
 * Panel that displays the Author's current papers associated
 * with the conference and allows access to add an additional
 * paper.
 * 
 * @author Paul Schultz (ps87@uw.edu)
 * @version 11/24/2012
 */
@SuppressWarnings("serial")
public class AuthorToolPanel extends JPanel
{
  
  /**
   * Constructs new AuthorTool Panel.
   * 
   * @param the_conf Conference data used to populate panel.
   * @param the_user User data used to populate panel.
   * @param the_frame data used change current (viewed) panel.
   */
  public AuthorToolPanel(final Conference the_conf, final User the_user, 
      final ConferenceWizardFrame the_frame)
  {
    super();
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(550, 600));
    setBackground(Color.WHITE);
    
    JLabel header = new JLabel("Author Tool");
    header.setForeground(Color.BLUE);
    header.setFont(new Font("Plain", Font.BOLD, 38));
    
    JPanel title = new JPanel(new FlowLayout(FlowLayout.LEADING));
    title.setBackground(Color.WHITE);
    title.add(header);
    
    JLabel instructions = new JLabel("To submit a new paper" +
    		" to this conference, please select this button:");
    instructions.setForeground(Color.RED);
    instructions.setFont(new Font("Plain", Font.PLAIN, 16));
    
    JPanel instruct_panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    instruct_panel.setBackground(Color.WHITE);
    
    JPanel north_panel = new JPanel();
    north_panel.setLayout(new BoxLayout(north_panel, BoxLayout.Y_AXIS));
    north_panel.setBackground(Color.WHITE);
    
    JButton submitPaper = new JButton("Paper Submission");
    submitPaper.addActionListener(new SubmitNewPaperListener(the_conf, the_frame, the_user));
    
    
    //Check if is past paper deadline. (BR21)
    if(the_conf.getDeadline().before(new Date(System.currentTimeMillis())))
    {
      instructions.setText("The Paper Submission deadline has passed for this conference.");
      submitPaper.setEnabled(false);
    }
    //Check for max # of papers submitted (BR15)
    else if (the_conf.getPapersByAuthor(the_user).size() >= 4) {
      instructions.setText("You're paper limit of 4 has been reached for this conference.");
      submitPaper.setEnabled(false);
    }
    instruct_panel.add(instructions);
    instruct_panel.add(submitPaper);
    
    north_panel.add(title);
    north_panel.add(Box.createVerticalStrut(5));
    north_panel.add(instruct_panel);
    
    add(north_panel, BorderLayout.NORTH);
    
    JPanel papers_panel = new JPanel();
    papers_panel.setLayout(new BoxLayout(papers_panel, BoxLayout.Y_AXIS));
    papers_panel.setBackground(Color.WHITE);
    
    JLabel pLabel = new JLabel("Submitted Papers:");
    pLabel.setFont(new Font("Plain", Font.PLAIN, 28));
    pLabel.setForeground(new Color(0, 102, 204));
    
    papers_panel.add(pLabel);
    papers_panel.add(Box.createVerticalStrut(20));
    
    //Populate the Author's submitted papers
    List<Model.Paper> papers = the_conf.getPapersByAuthor(the_user);
    for (Model.Paper p : papers) 
    {
      papers_panel.add(new PaperPanel(p, the_conf, the_user, the_frame));
      papers_panel.add(Box.createVerticalStrut(10));
    }
    
    JScrollPane scroll_panel = new JScrollPane(papers_panel);
    add(scroll_panel, BorderLayout.CENTER);
    
  }
}
